#!/bin/bash
# This is a ShellScript For Auto Install LNMP and Moodle on CentOS
# Add executive privilege
# chmod 755 centos.sh
# Powered by hongm
# Visted http://hongm.emoodle.org
# Modify 2019-06-18
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

## Moodle Version##
Moodle_Version="3.9"

##Download Server##
Download_Mirror="http://hongm.emoodle.org/down"
##Moodle WebSite Home Directory##
Default_Moodle_Dir="/www"
##Moodle Code##
MoodleFileName="moodle_git.tgz"
##MariaDB database directory##
MariaDB_Data_Dir="${Default_Moodle_Dir}/mariadb"


#ConfigFiles
NginxConfigFile="/etc/nginx/nginx.conf"
Nginx_User="nginx"
MariaDBConfigFile="/etc/my.cnf.d/mariadb-server.cnf"
PHPConfigFile="/etc/php.ini"
FPMConfigFile="/etc/php-fpm.d/www.conf"

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script"
    exit 1
fi

clear
echo ""
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
    if grep -Eqi "release 8." /etc/redhat-release; then
        CentOS_Ver='8'
        CentOS_Version=$(cat /etc/redhat-release | awk -F " " '{print $4}')
        #Check IP Configure File
        Network_Config=$( ls /etc/sysconfig/network-scripts/ | grep ifcfg-en )
        echo -e " \e[0;33mYour Current CentOS Version: \e[0;31m${CentOS_Version}\e[0m"
        IP=$(ip addr | grep inet | grep brd | awk '{ print $2; }' | sed 's/\/.*$//')
     else
        echo -e " \e[0;31mYour OS is not CentOS 8 !\e[0m"
        exit 1
    fi
 else
    echo -e " \e[0;31mYour OS is not CentOS!\e[0m"
    exit 1
fi

#set MariaDB root password
echo "======================================================="
DB_Root_PWD="123456"
echo -e " \e[0;33mPlease setup root password of MariaDB.(Default password: 123456)\e[0m"
read -p "Please enter: " DB_Root_PWD
if [ "${DB_Root_PWD}" = "" ]; then
     DB_Root_PWD="123456"
fi
echo -e " \e[0;32mMySQL root password: \e[0;31m${DB_Root_PWD}\e[0m"

#set Moodle admin email
echo "======================================================="
MoodleAdminEmail="admin@163.com"
echo -e " \e[0;33mPlease setup E-mail of Moodle Admin.(Default email: admin@163.com)\e[0m"
read -p "Please enter: " MoodleAdminEmail
if [ "${MoodleAdminEmail}" = "" ]; then
     MoodleAdminEmail="admin@163.com"
fi
echo -e " \e[0;32mMoodle admin E-mail: \e[0;31m${MoodleAdminEmail}\e[0m"

#Moodle_Selection
    MoodleVer=${Moodle_Version:0:1}${Moodle_Version:2:1}
    MoodleVer8=${MoodleVer}
    MoodleVer7=$[${MoodleVer}-1]
    MoodleVer6=$[${MoodleVer}-2]
    MoodleVer5=$[${MoodleVer}-3]
    MoodleVer4=$[${MoodleVer}-4]
    MoodleVer3=$[${MoodleVer}-5]
    MoodleVer2=$[${MoodleVer}-6]
    MoodleVer1=$[${MoodleVer}-7]
    #which Moodle Version do you want to install?
    echo "==========================="
    Moodle_Ver8=${MoodleVer8:0:1}.${MoodleVer8:1:1}
    Moodle_Ver7=${MoodleVer7:0:1}.${MoodleVer7:1:1}
    Moodle_Ver6=${MoodleVer6:0:1}.${MoodleVer6:1:1}
    Moodle_Ver5=${MoodleVer5:0:1}.${MoodleVer5:1:1}
    Moodle_Ver4=${MoodleVer4:0:1}.${MoodleVer4:1:1}
    Moodle_Ver3=${MoodleVer3:0:1}.${MoodleVer3:1:1}
    Moodle_Ver2=${MoodleVer2:0:1}.${MoodleVer2:1:1}
    Moodle_Ver1=${MoodleVer1:0:1}.${MoodleVer1:1:1}
    MoodleSelect="8"
    echo -e " \e[0;33mYou have many options for your Moodle install.\e[0m"
    echo -e "   \e[0;31m1\e[0m: Install Moodle ${Moodle_Ver1} (Legacy releases)"
    echo -e "   \e[0;31m2\e[0m: Install Moodle ${Moodle_Ver2} (Legacy releases)"
    echo -e "   \e[0;31m3\e[0m: Install Moodle ${Moodle_Ver3} (Legacy releases)"
    echo -e "   \e[0;31m4\e[0m: Install Moodle ${Moodle_Ver4} (Legacy releases)"
    echo -e "   \e[0;31m5\e[0m: Install Moodle ${Moodle_Ver5} (Security-only-supported releases)"
    echo -e "   \e[0;31m6\e[0m: Install Moodle ${Moodle_Ver6} (Security-only-supported releases)"
    echo -e "   \e[0;31m7\e[0m: Install Moodle ${Moodle_Ver7} (Other supported releases)"
    echo -e "   \e[0;31m8\e[0m: Install Moodle ${Moodle_Ver8} (Latest release)\e[44;37m(Default)\e[0m"
    read -p "Enter your choice (1, 2, 3, 4, 5, 6, 7 or 8): " MoodleSelect
    if [ "${MoodleSelect}" = "" ]; then
        MoodleSelect="8"
    fi
    CurMoodleVer=$[MoodleVer${MoodleSelect}]
    MoodleVer="MOODLE_${CurMoodleVer}_STABLE"
    Moodle_Version=${CurMoodleVer:0:1}.${CurMoodleVer:1:1}
    #echo -e " \e[0;32mNo input,You will install \e[0;31mMoodle ${Moodle_Version}\e[0m"

echo ""
echo ""
echo ""
echo ""
echo -e "\e[0;33mYou will install:\e[0m"
echo -e "    \e[0;31mNginx\t\e[0;32m1.14\e[0m"
echo -e "    \e[0;31mMariaDB\t\e[0;32m10.4\e[0m"
echo -e "    \e[0;31mPHP\t\t\e[0;32m7.4\e[0m"
echo -e "    \e[0;31mMoodle\t\e[0;32m${Moodle_Version}\e[0m"
echo ""
echo "Press any key to install...or Press Ctrl+c to cancel"
OLDCONFIG=`stty -g`
stty -icanon -echo min 1 time 0
dd count=1 2>/dev/null
stty ${OLDCONFIG}


if [ "${Download_Mirror}" = "http://hongm.emoodle.org/down" ]; then
    mirror_code=`curl -o /dev/null -m 20 --connect-timeout 20 -sk -w %{http_code} http://hongm.emoodle.org/down/`
    if [ "${mirror_code}" != "200" ]; then
        mirror_code=`curl -o /dev/null -m 20 --connect-timeout 20 -sk -w %{http_code} https://study.wjszzx.com.cn/soft/`
        if [ "${mirror_code}" = "200" ]; then
            echo "Change to mirror https://study.wjszzx.com.cn/soft"
            Download_Mirror='https://study.wjszzx.com.cn/soft'
         else
            echo "Dwonload Server Error!"
            exit 1
        fi
    fi
fi

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
if cat /etc/selinux/config | grep "^SELINUX=enforcing" ; then
    sed -i 's:SELINUX=enforcing:#SELINUX=enforcing\nSELINUX=disabled:g' /etc/selinux/config
    setenforce 0
fi

# Modify repo
sed -i 's:mirrorlist=:#mirrorlist=:g' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's:#baseurl=:baseurl=:g' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's:mirror.centos.org:mirrors.ustc.edu.cn:g' /etc/yum.repos.d/CentOS-Base.repo
# Install REMI and EPEL repo
dnf -y install http://mirrors.ustc.edu.cn/epel/epel-release-latest-8.noarch.rpm
dnf -y install http://mirrors.ustc.edu.cn/remi/enterprise/remi-release-8.rpm
sed -i 's:mirrorlist=:#mirrorlist=:g' /etc/yum.repos.d/epel.repo
sed -i 's:#baseurl=:baseurl=:g' /etc/yum.repos.d/epel.repo
sed -i 's:download.fedoraproject.org/pub:mirrors.ustc.edu.cn:g' /etc/yum.repos.d/epel.repo
sed -i 's:mirrorlist=:#mirrorlist=:g' /etc/yum.repos.d/remi.repo
sed -i 's:#baseurl=:baseurl=:g' /etc/yum.repos.d/remi.repo
sed -i 's:rpms.remirepo.net:mirrors.ustc.edu.cn/remi:g' /etc/yum.repos.d/remi.repo
# Add MariaDB mirrors
cat > /etc/yum.repos.d/MariaDB.repo<<EOF
[mariadb]
name=MariaDB
baseurl=http://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos8-amd64
module_hotfixes=1
gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

rm -fr /var/cache/yum/x86_64
rm -fr /var/cache/dnf/x86_64
dnf -y makecache
dnf -y update
dnf -y install git wget nano


echo -e " \e[0;31m[+] Yum installing Nginx+MariaDB+PHP ...\e[0m"
dnf -y module reset php
dnf -y module enable php:remi-7.4
n=0
flag=1
while [ $n -le 8 -a $flag -gt 0 ]
do
    dnf -y install mariadb-server nginx php php-fpm php-mysqlnd php-mbstring php-gd php-xml php-xmlrpc php-curl php-xsl php-intl php-opcache php-soap php-zip php-tidy php-pear
    flag=$?
    n=$((n+1))
done
if [ $n -ge 8 ]; then
    echo -e "\e[0;31m[+] Dnf Error!\e[0m"
    exit 1
fi

echo -e " \e[0;32m[+] Modify MariaDB config ...\e[0m"
systemctl restart mariadb.service
/usr/bin/mysqladmin -u root password ${DB_Root_PWD}
cat > /tmp/mariadb_sec_script<<EOF
grant all on moodle.* to 'moodle'@'localhost' identified by '${DB_Root_PWD}';
grant all on moodle.* to 'moodle'@'127.0.0.1' identified by '${DB_Root_PWD}';
CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
flush privileges;
EOF
/usr/bin/mysql -u root -p${DB_Root_PWD} -h localhost < /tmp/mariadb_sec_script
rm -f /tmp/mariadb_sec_script
systemctl stop mariadb.service
mkdir -p ${MariaDB_Data_Dir}/logs
cp -a /var/lib/mysql ${MariaDB_Data_Dir}/data
chown -R mysql:mysql ${MariaDB_Data_Dir}/data
chown -R mysql:mysql ${MariaDB_Data_Dir}/logs
cat > ${MariaDBConfigFile}<<EOF
[client]
socket	= ${MariaDB_Data_Dir}/data/mysql.sock
[mysqld]
port	= 3306
socket	= ${MariaDB_Data_Dir}/data/mysql.sock
user    = mysql
basedir = /usr
datadir = ${MariaDB_Data_Dir}/data
log_error = ${MariaDB_Data_Dir}/logs/mariadb.err
pid-file = ${MariaDB_Data_Dir}/data/mariadb.pid
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
table_open_cache = 64
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=${MariaDB_Data_Dir}/logs/mariadb-bin
binlog_format=mixed
server-id	= 1
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_data_home_dir = ${MariaDB_Data_Dir}/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = ${MariaDB_Data_Dir}/data
innodb_buffer_pool_size = 128M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_file_per_table	= 1
innodb_flush_method	= O_DIRECT
EOF
systemctl restart mariadb.service
systemctl enable mariadb.service


echo -e " \e[0;32m[+] Modify PHP config ...\e[0m"
sed -i 's/post_max_size = 8M/post_max_size = 2048M/g' ${PHPConfigFile}
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 2048M/g' ${PHPConfigFile}
sed -i 's/;date.timezone =/date.timezone = PRC/g' ${PHPConfigFile}
sed -i 's/short_open_tag = Off/short_open_tag = On/g' ${PHPConfigFile}
sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' ${PHPConfigFile}
sed -i 's/max_execution_time = 30/max_execution_time = 300/g' ${PHPConfigFile}
sed -i 's/max_input_time = 60/max_input_time = 600/g' ${PHPConfigFile}
sed -i 's/default_socket_timeout = 60/default_socket_timeout = 600/g' ${PHPConfigFile}
sed -i 's/memory_limit = 128M/memory_limit = 4096M/g' ${PHPConfigFile}
sed -i 's/user = apache/user = nginx/g' ${FPMConfigFile}
sed -i 's/group = apache/group = nginx/g' ${FPMConfigFile}
systemctl restart php-fpm.service
systemctl enable php-fpm.service


echo -e " \e[0;32m[+] Modify Nginx config ...\e[0m"
echo "user  nginx;" > ${NginxConfigFile}
echo "worker_processes  auto;" >> ${NginxConfigFile}
echo "worker_rlimit_nofile  65535;" >> ${NginxConfigFile}
echo "error_log  /var/log/nginx/error.log warn;" >> ${NginxConfigFile}
echo "pid        /var/run/nginx.pid;" >> ${NginxConfigFile}
echo "include /usr/share/nginx/modules/*.conf;" >> ${NginxConfigFile}
echo "events {" >> ${NginxConfigFile}
echo "    worker_connections  10240;" >> ${NginxConfigFile}
echo "    use epoll;" >> ${NginxConfigFile}
echo "}" >> ${NginxConfigFile}
echo "http {" >> ${NginxConfigFile}
echo "    include       /etc/nginx/mime.types;" >> ${NginxConfigFile}
echo "    access_log  /var/log/nginx/access.log;" >> ${NginxConfigFile}
echo "    sendfile        on;" >> ${NginxConfigFile}
echo "    tcp_nopush     on;" >> ${NginxConfigFile}
echo "    client_max_body_size 2048m;" >> ${NginxConfigFile}
echo "    client_header_buffer_size 32k;" >> ${NginxConfigFile}
echo "    large_client_header_buffers 4 32k;" >> ${NginxConfigFile}
echo "    server_names_hash_bucket_size 128;" >> ${NginxConfigFile}
echo "    fastcgi_connect_timeout 300;" >> ${NginxConfigFile}
echo "    fastcgi_send_timeout 300;" >> ${NginxConfigFile}
echo "    fastcgi_read_timeout 300;" >> ${NginxConfigFile}
echo "    fastcgi_buffer_size 64k;" >> ${NginxConfigFile}
echo "    fastcgi_buffers 4 64k;" >> ${NginxConfigFile}
echo "    fastcgi_busy_buffers_size 128k;" >> ${NginxConfigFile}
echo "    fastcgi_temp_file_write_size 256k;" >> ${NginxConfigFile}
echo "    keepalive_timeout  65;" >> ${NginxConfigFile}
echo "    include /etc/nginx/conf.d/*.conf;" >> ${NginxConfigFile}
echo "    server {" >> ${NginxConfigFile}
echo "        listen       80 default_server;" >> ${NginxConfigFile}
echo "        listen       [::]:80 default_server;" >> ${NginxConfigFile}
echo "        server_name  _;" >> ${NginxConfigFile}
echo "        root   /www/moodle;" >> ${NginxConfigFile}
echo "        include /etc/nginx/default.d/*.conf;" >> ${NginxConfigFile}
echo "        error_page   500 502 503 504  /50x.html;" >> ${NginxConfigFile}
echo "        location = /50x.html {" >> ${NginxConfigFile}
echo "            root   /usr/share/nginx/html;" >> ${NginxConfigFile}
echo "        }" >> ${NginxConfigFile}
echo '        if (!-e $request_filename) {' >> ${NginxConfigFile}
echo '            rewrite "^(.*\.php)(/)(.*)$" $1?file=/$3 last;' >> ${NginxConfigFile}
echo "        }" >> ${NginxConfigFile}
echo "    }" >> ${NginxConfigFile}
echo "}" >> ${NginxConfigFile}

systemctl restart nginx.service
systemctl enable nginx.service


echo -e " \e[0;32m[+] Install Moodle Soucre...\e[0m"
mkdir -p ${Default_Moodle_Dir}
rm -fr ${Default_Moodle_Dir}/moodle
cd ${Default_Moodle_Dir}
n=0
while [ $n -le 8 -a ! -f ${Default_Moodle_Dir}/moodle/version.php ]
do
    git clone -b ${MoodleVer} --depth=1 https://gitee.com/hongm/moodle.git
    n=$((n+1))
done
echo '<?PHP  // Moodle configuration file' > ${Default_Moodle_Dir}/moodle/config.php
echo 'unset($CFG);' >> ${Default_Moodle_Dir}/moodle/config.php
echo 'global $CFG;' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG = new stdClass();' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->dbtype    = "mariadb";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->dblibrary = "native";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->dbhost    = "127.0.0.1";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->dbname    = "moodle";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->dbuser    = "moodle";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->dbpass    = "'${DB_Root_PWD}'";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->prefix    = "mdl_";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->dboptions = array(' >> ${Default_Moodle_Dir}/moodle/config.php
echo '  "dbpersist" => 0,' >> ${Default_Moodle_Dir}/moodle/config.php
echo '  "dbport" => "",' >> ${Default_Moodle_Dir}/moodle/config.php
echo '  "dbsocket" => "",' >> ${Default_Moodle_Dir}/moodle/config.php
echo '  "dbcollation" => "utf8mb4_unicode_ci",' >> ${Default_Moodle_Dir}/moodle/config.php
echo ');' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->wwwroot   = "http://'${IP}'";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->dataroot  = "'${Default_Moodle_Dir}'/moodledata";' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->directorypermissions = 0700;' >> ${Default_Moodle_Dir}/moodle/config.php
echo '$CFG->admin = "admin";' >> ${Default_Moodle_Dir}/moodle/config.php
echo 'require_once(dirname(__FILE__) . "/lib/setup.php"); // Do not edit' >> ${Default_Moodle_Dir}/moodle/config.php
mkdir -p ${Default_Moodle_Dir}/moodledata
chown -R ${Nginx_User}:${Nginx_User} ${Default_Moodle_Dir}/moodle
chown -R ${Nginx_User}:${Nginx_User} ${Default_Moodle_Dir}/moodledata
chmod -R 0700 ${Default_Moodle_Dir}/moodle
chmod -R 0700 ${Default_Moodle_Dir}/moodledata
find ${Default_Moodle_Dir}/moodle -type f -exec chmod 0600 {} \;

n=0
while [ $n -le 8 -a ! -f ${MariaDB_Data_Dir}/data/moodle/mdl_user.frm ]
do
    sudo -u ${Nginx_User} /usr/bin/php ${Default_Moodle_Dir}/moodle/admin/cli/install_database.php --lang=zh_cn --adminpass=${DB_Root_PWD} --adminemail=${MoodleAdminEmail} --fullname=ITCenter --shortname=ITCenter --agree-license
    n=$((n+1))
done
if [ $n -ge 8 ]; then
    echo "Network error! Please continue to install http://${IP} in the browser!"
 else
    /usr/bin/mysql -u root -p${DB_Root_PWD} -h localhost -e "UPDATE moodle.mdl_config SET value='firstname,lastname' WHERE name='alternativefullnameformat';"
    /usr/bin/mysql -u root -p${DB_Root_PWD} -h localhost -e "UPDATE moodle.mdl_config SET value='0' WHERE name='defaulthomepage';"
    /usr/bin/mysql -u root -p${DB_Root_PWD} -h localhost -e "UPDATE moodle.mdl_config SET value='CN' WHERE name='country';"
    /usr/bin/mysql -u root -p${DB_Root_PWD} -h localhost -e "UPDATE moodle.mdl_config SET value='Asia/Shanghai' WHERE name='timezone';"
    /usr/bin/mysql -u root -p${DB_Root_PWD} -h localhost -e "UPDATE moodle.mdl_config SET value='' WHERE name='docroot';"
    /usr/bin/mysql -u root -p${DB_Root_PWD} -h localhost -e "UPDATE moodle.mdl_config SET value='' WHERE name='noreplyaddress';"
    /usr/bin/php ${Default_Moodle_Dir}/moodle/admin/cli/mysql_compressed_rows.php -f
    echo "* * * * * /usr/bin/php /www/moodle/admin/cli/cron.php >/tmp/moodlecron.log" >> /var/spool/cron/${Nginx_User}
    chown ${Nginx_User}:${Nginx_User} /var/spool/cron/${Nginx_User}
    chmod 0600 /var/spool/cron/${Nginx_User}
    wget -c --progress=bar:force --no-check-certificate ${Download_Mirror}/moodle/langpack/zh_cn_local.tar.gz
    if  [ -s zh_cn_local.tar.gz ]; then
        tar zxf zh_cn_local.tar.gz -C ${Default_Moodle_Dir}/moodledata/lang
        chown -R ${Nginx_User}:${Nginx_User} ${Default_Moodle_Dir}/moodledata/lang/zh_cn_local
        chmod -R 0700 ${Default_Moodle_Dir}/moodledata/lang/zh_cn_local
        rm -fr zh_cn_local.tar.gz
    fi
fi
if [ ! -f /usr/local/bin/moodle ]; then
    if [ ! -f ${cur_dir}/conf/init.d.moodle ]; then
        wget -c --progress=bar:force --no-check-certificate ${Download_Mirror}/moodle/imoodle/init.d.moodle -O /usr/local/bin/moodle
     else
        cp ${cur_dir}/conf/init.d.moodle /usr/local/bin/moodle
    fi
    if [ -f /usr/local/bin/moodle ]; then
        chmod 0700 /usr/local/bin/moodle
    fi
fi

#Check_Files
isNginx=""
echo "============================== Check install =============================="
echo "Checking ..."
if [[ -s ${NginxConfigFile} && -s /usr/sbin/nginx ]]; then
    echo -e " \e[0;32mNginx: OK\e[0m"
    isNginx="ok"
 else
    echo -e " \e[0;31mError: Nginx install failed.\e[0m"
fi
isDB=""
if [[ -s /usr/bin/mysql && -s /usr/bin/mysqld_safe && -s ${MariaDBConfigFile} ]]; then
    echo -e " \e[0;32mMariaDB: OK\e[0m"
    isDB="ok"
 else
    echo -e " \e[0;31mError: MariaDB install failed.\e[0m"
fi
isPHP=""
if [[ -s /usr/sbin/php-fpm && -s ${PHPConfigFile} && -s /usr/bin/php ]]; then
    echo -e " \e[0;32mPHP: OK\e[0m"
    isPHP="ok"
 else
    echo -e " \e[0;31mError: PHP install failed.\e[0m"
fi
if [[ "$isNginx" = "ok" && "$isDB" = "ok" && "$isPHP" = "ok" ]]; then
    echo "------------------------------------------------------------------------"
    echo "      moodle ${Moodle_Version} for CentOS ${CentOS_Ver} Linux Server, Written by Hongm"
    echo "                 http://hongm.emoodle.org                               "
    echo "------------------------------------------------------------------------"
    echo "  Nginx:   systemctl {start|stop|reload|restart|status} nginx.service   "
    echo "  MariaDB: systemctl {start|stop|reload|restart|status} mariadb.service "
    echo "  PHP-FPM: systemctl {start|stop|reload|restart|status} php-fpm.service "
    echo "------------------------------------------------------------------------"
    echo "  Moodle Web Site: http://${IP}                                            "
    echo "  Moodle Soucre directory: ${Default_Moodle_Dir}/moodle                 "
    echo "  Moodle Data directory: ${Default_Moodle_Dir}/moodledata               "
    echo "------------------------------------------------------------------------"
    echo "  Moodle admin password: ${DB_Root_PWD}                                 "
    echo "------------------------------------------------------------------------"
    echo "  MariaDB root password: ${DB_Root_PWD}                                 "
    echo "------------------------------------------------------------------------"
 else
    echo -e " \e[0;31mSorry,Failed to install LNMP!\e[0m"
    echo -e " \e[0;31mYou can download moodle-install.log from your server,and mail install.log to hongm@sina.com.\e[0m"
fi
